package com.fulihui.wgj.service.statistics;

import com.alibaba.dubbo.config.annotation.Service;
import com.fulihui.common.builder.rpc.RpcResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.error.Errors;
import com.fulihui.common.error.InvokeServiceException;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.ConditionValidator;
import com.fulihui.wgj.dal.mybatis.domain.WxSubscribeStatistics;
import com.fulihui.wgj.dal.mybatis.repository.WxSubscribeStatisticsRepository;
import com.fulihui.wgj.facade.dto.WxSubscribeStatisticsDTO;
import com.fulihui.wgj.facade.request.statistics.WxSubscribeStatisticsPageRequest;
import com.fulihui.wgj.facade.request.statistics.WxSubscribeStatisticsQueryRequest;
import com.fulihui.wgj.facade.request.statistics.WxSubscribeStatisticsRequest;
import com.fulihui.wgj.facade.result.statistics.WxSubscribeStatisticsPageResult;
import com.fulihui.wgj.facade.service.statistics.WxSubscribeStatisticsService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 微信粉丝七日留存Service
 *
 * @author wang_wx
 * @date 2018-07-03
 */
@Component
@Service(interfaceClass = WxSubscribeStatisticsService.class, version = "1.0.0")
public class WxSubscribeStatisticsServiceImpl implements WxSubscribeStatisticsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSubscribeStatisticsServiceImpl.class);

    @Autowired
    WxSubscribeStatisticsRepository wxSubscribeStatisticsRepository;

    @Override
    public BaseResult saveWxStatistics(WxSubscribeStatisticsRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeStatistics subscribeStatistics = BeanConv.copy(request, WxSubscribeStatistics.class);
        int exist = wxSubscribeStatisticsRepository.countByAppIdAndDate(subscribeStatistics);
        if (exist > 0) {
            return RpcResultBuilder.fail(Errors.WxSevenRetentionError.RECORD_ALREADY_EXISTS);
        }
        Integer count = wxSubscribeStatisticsRepository.saveSelective(subscribeStatistics);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.SAVE_FAILED);
    }

    @Override
    public BaseResult updateByAppIdAndDate(WxSubscribeStatisticsRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeStatistics subscribeStatistics = BeanConv.copy(request, WxSubscribeStatistics.class);
        int count = wxSubscribeStatisticsRepository.updateByAppIdAndDate(subscribeStatistics);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.UPDATE_FAILED);
    }

    @Override
    public SimpleResult<List<WxSubscribeStatisticsDTO>> queryWxStatistics(WxSubscribeStatisticsQueryRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        SimpleResult result = new SimpleResult<List<WxSubscribeStatisticsDTO>>();
        List<WxSubscribeStatistics> subscribeStatistics = wxSubscribeStatisticsRepository.queryList(request.getAppId(), request.getBeginDate(), request.getEndDate());
        if (!CollectionUtils.isEmpty(subscribeStatistics)) {
            result = RpcResultBuilder.succSimple(BeanConv.copy(subscribeStatistics, WxSubscribeStatisticsDTO.class));
        }
        return result;
    }

    @Override
    public WxSubscribeStatisticsPageResult queryPageWxStatistics(WxSubscribeStatisticsPageRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        PageInfo<WxSubscribeStatistics> pageInfo = wxSubscribeStatisticsRepository.queryPageList(request.getAppId(), request.getBeginDate(), request.getEndDate(), request.getPage(), request.getRows());
        long total = pageInfo.getTotal();
        WxSubscribeStatisticsPageResult result = RpcResultBuilder.succPageResult(WxSubscribeStatisticsPageResult.class, pageInfo.getPageNum(), pageInfo.getPageSize(), Math.toIntExact(total));
        if (total > 0) {
            result.setValue(BeanConv.copy(pageInfo.getList(), WxSubscribeStatisticsDTO.class));
        }
        return result;
    }
}
